package com.huidz.calendar.activity;

import android.content.res.Resources;
import android.os.Bundle;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.huidz.calendar.R;
import com.huidz.calendar.ViewPagerAdapter;
import com.huidz.calendar.luckfragment.ThismonthFragment;
import com.huidz.calendar.luckfragment.ThisweekFragment;
import com.huidz.calendar.luckfragment.ThisyearFragment;
import com.huidz.calendar.luckfragment.ToDayFragment;
import com.huidz.calendar.luckfragment.TomorrowFragment;

import java.util.ArrayList;
import java.util.List;

public class ConstellationActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager home_pager;
    private List<Fragment> fraglist;

    //ViewPager适配器
    private ViewPagerAdapter pagerAdapter;
    //底部导航栏的四个rela
    /*****************************/
    private TabLayout mytab;
    private ImageView comeback;
    //共享给自己的fragment的数据
//    public static String
    private int tabCount = 5;
    public static final int MOVABLE_COUNT = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.RIGHT);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.activity_constellation);
        inti();
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        home_pager = findViewById(R.id.constellation_pager);
        comeback = findViewById(R.id.title_comeback);
        comeback.setOnClickListener(this);
        AddFragment();
        mytab = findViewById(R.id.mytab);
        mytab.setTabMode(tabCount <= MOVABLE_COUNT ? TabLayout.MODE_FIXED : TabLayout.MODE_SCROLLABLE);
        mytab.addTab(mytab.newTab().setText("今日运势"));
        mytab.addTab(mytab.newTab().setText("明日运势"));
        mytab.addTab(mytab.newTab().setText("本周运势"));
        mytab.addTab(mytab.newTab().setText("本月运势"));
        mytab.addTab(mytab.newTab().setText("本年运势"));
        mytab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 开启Fragment事务
                if (tab.getText().equals("今日运势")) {
                    home_pager.setCurrentItem(0);
                }
                if (tab.getText().equals("明日运势")) {
                    home_pager.setCurrentItem(1);
                }
                if (tab.getText().equals("本周运势")) {
                    home_pager.setCurrentItem(2);
                }
                if (tab.getText().equals("本月运势")) {
                    home_pager.setCurrentItem(3);
                }
                if (tab.getText().equals("本年运势")) {
                    home_pager.setCurrentItem(4);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            mytab.getTabAt(position).select();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    /**
     * 用于绑定fragment
     */
    public void AddFragment() {
        //添加frag到集合中
        fraglist = new ArrayList<>();
        fraglist.add(new ToDayFragment());
        fraglist.add(new TomorrowFragment());
        fraglist.add(new ThisweekFragment());
        fraglist.add(new ThismonthFragment());
        fraglist.add(new ThisyearFragment());
        //绑定点击事件
        home_pager.setOnPageChangeListener(new MyPagerChangeListener());
        //绑定适配器
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fraglist);
        home_pager.setAdapter(pagerAdapter);
        home_pager.setCurrentItem(0);
        home_pager.setOffscreenPageLimit(5);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_comeback:
                onBackPressed();
                break;
        }
    }

    public static long firstTime = 0;


}
